//
//  MenuView.m
//  MindBox
//
//  Created by MS on 16-1-12.
//  Copyright (c) 2016年 Lemon. All rights reserved.
//
#define SLIP_VIEW_HEIGHT 1.5
#import "MenuView.h"
#import "UIView+Frame.h"
@interface MenuView ()
@property (nonatomic,strong) NSMutableArray * btnArray;


@property (nonatomic,assign) BOOL flag;
@property (nonatomic,weak) UIView * slipView;
@property (nonatomic,weak) UIView * lineView;
@end

@implementation MenuView

-(void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    
    int i = 0;
    for (NSString * title in titleArray) {
        
       [self setUpOneButtonWithTitle:title tag:i];
        i++;
    }
    
    [self layoutSubviews];
}

-(NSMutableArray *)btnArray
{
    if (_btnArray==nil) {
        _btnArray = [NSMutableArray array];
    }
    return _btnArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIView *lineView = [[UIView alloc] init];
        [self addSubview:lineView];
        lineView.backgroundColor = [UIColor lightGrayColor];
        self.lineView = lineView;
        
        self.backgroundColor =[UIColor whiteColor];
        UIView *slipView = [[UIView alloc] init];
        [self addSubview:slipView];
        slipView.backgroundColor = [UIColor redColor];
        self.slipView = slipView;
        
    }
    return self;
}




-(UIButton *)setUpOneButtonWithTitle:(NSString *)title tag:(NSInteger)tag
{
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:btn];
    
    [self.btnArray addObject:btn];
    btn.tag = tag;
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    btn.imageEdgeInsets = UIEdgeInsetsMake(0, 40, 0, 0);
    btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 15);
    btn.adjustsImageWhenHighlighted = NO;

    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted|UIControlStateSelected];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [btn setHighlighted:NO];
    
    if (tag==1) {
        [btn setImage:[UIImage imageNamed:@"btn_normal"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"btn_up"] forState:UIControlStateSelected];
    }
    else if (tag==4){
        [btn setImage:[UIImage imageNamed:@"btn_sel_nor"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"btn_sel_hightligh"] forState:UIControlStateSelected];
    }
    
    if (tag==0) {
        [self btnClick:btn];
    }
    
       return btn;
}

-(void)btnClick:(UIButton *)btn
{
    
    
    if (btn.tag==1 &&_selBtn.tag==1) {
        
            if ((self.flag=!_flag)) {//flag 1 降序
                [btn setImage:[UIImage imageNamed:@"btn_down"] forState:UIControlStateSelected];
            }
            else{//flag 0 升序
                [btn setImage:[UIImage imageNamed:@"btn_up"] forState:UIControlStateSelected];
            }
    }else if(btn.tag==1 &&_selBtn.tag!=1){
        _flag = NO;
       [btn setImage:[UIImage imageNamed:@"btn_up"] forState:UIControlStateSelected];
    }
    
    if (btn.tag!=4) {
        _speBtn.selected = NO;
        _selBtn.selected = NO;
        btn.selected = YES;
        _selBtn = btn;
        
        [UIView animateWithDuration:0.25 animations:^{
            self.slipView.frame = CGRectMake(btn.x+btn.titleLabel.x, self.height-SLIP_VIEW_HEIGHT, btn.titleLabel.width, SLIP_VIEW_HEIGHT);
        }];
    }
    
    if ([self.delegate respondsToSelector:@selector(menuViewDidClick:index:type:)]) {
        [self.delegate menuViewDidClick:self index:btn.tag type:_flag];
    }

    if (btn.tag==4) {
        btn.selected = !btn.selected;
        _speBtn = btn;
    }
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    self.lineView.frame = CGRectMake(0, self.height-0.5, self.width, 0.5);
    CGFloat y = 0;
    CGFloat w = (self.width-10-5*4)/5;
    CGFloat h = self.height-2;
    int i =0;
    for (UIButton * btn in self.btnArray) {
        CGFloat x = 5+i * (5+w);
        btn.frame = CGRectMake(x, y, w, h);
     
        if (i==0) {
            self.slipView.frame = CGRectMake(btn.x+btn.titleLabel.x, self.height-SLIP_VIEW_HEIGHT, btn.titleLabel.width, SLIP_VIEW_HEIGHT);
        }
        
        i++;
    }
    
}


@end
